package com.bkgy.modular.samePlateDifference.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.excelhandle.PlanExcelHandlerImpl;
import com.bkgy.modular.samePlateDifference.entity.Difference;
import com.bkgy.modular.samePlateDifference.entity.DifferenceCure;
import com.bkgy.modular.samePlateDifference.entity.ExportDifference;
import com.bkgy.modular.samePlateDifference.mapper.DifferenceMapper;
import com.bkgy.modular.samePlateDifference.service.DifferenceService;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.ExcelUtilsPOI;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @version 1.0
 * @ClassName DifferenceServiceImpl
 * @date 2023/2/16 15:42
 */
@Service
public class DifferenceServiceImpl extends ServiceImpl<DifferenceMapper, Difference> implements DifferenceService {
    @Resource
    private DifferenceMapper differenceMapper;
    @Override
    public ResultData findDifferenceData(JSONObject jsonObject) {
        Integer pageIndex = jsonObject.getInteger("pageIndex");
        Integer pageSize = jsonObject.getInteger("pageSize");
        if (pageIndex == null && pageSize == null) {
            return ResultData.ResultDataFaultSelf("分页参数为空",-1);
        }
        // 接收前端传的数据
        JSONObject condition = jsonObject.getJSONObject("condition");
        List<Difference> list = differenceMapper.findByData((pageIndex - 1) * pageSize, pageIndex * pageSize,condition);
        Integer count = differenceMapper.findDataCount(condition);
        return ResultData.ResultDataSuccess(list,count);
    }

    @Override
    public void export(JSONObject jsonObject, HttpServletResponse response) {
        // 接收传入参数
        Integer pageSize = jsonObject.getInteger("pageSize");
        Integer pageCount = jsonObject.getInteger("pageCount");
        String fileNames = jsonObject.getString("fileName");
        JSONObject condition = jsonObject.getJSONObject("condition");
        // 创建表格数据集合
        List<Difference> listExcel = new ArrayList<>();// 调用service查询出数据
        List<Difference> data = null;
        List<ExportDifference> list = new ArrayList<>();
        for (int i = 1; i <= pageCount; i++) {
            // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
            data = differenceMapper.findByData((i - 1) * pageSize, i * pageSize,condition);
            if (!data.isEmpty() && data.size() > 0) {
                for (Difference item : data) {
                    ExportDifference exportDifference = new ExportDifference();
                    BeanUtils.copyProperties(item,exportDifference);
                    list.add(exportDifference);
                }
            }
        }
        try {
            Map<String,Object> data1 = new HashMap();
            data1.put("data",list);
            File file = ResourceUtils.getFile("classpath:exceltemplate\\同板差预测.xlsx");
            TemplateExportParams templateExportParams = new TemplateExportParams(file.getPath(),false);
            templateExportParams.setDictHandler(new PlanExcelHandlerImpl());
            Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams, data1);
            ExcelUtilsPOI.exportExcel(fileNames,response,workbook);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public ResultData findDifferenceCure(JSONObject jsonObject) {
        // 接收传入参数
        JSONObject condition = jsonObject.getJSONObject("condition");
        if (condition.isEmpty()) {
            return ResultData.ResultDataFaultSelf("请检查参数是否正确",-1);
        }
        List<DifferenceCure> differenceCure = differenceMapper.findDifferenceCure(condition);
        return ResultData.ResultDataSuccess(differenceCure,differenceCure.size());
    }
}
